//
//  KATFrameDisplayer.m
//  KATFramework
//
//  Created by Kantice on 16/7/20.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATFrameDisplayer.h"

@implementation KATFrameDisplayer

#pragma -mark 类方法

///获取实例
+ (instancetype)displayerWithFrame:(CGRect)frame
{
    KATFrameDisplayer *displayer=[[[self alloc] init] autorelease];
    
    [displayer initSprite];
    
    displayer.userInteractionEnabled=NO;
    displayer.frame=frame;
    
    displayer.label=[[[UILabel alloc] init] autorelease];
    displayer.label.frame=displayer.bounds;
    displayer.label.text=@"";
    displayer.label.textAlignment=NSTextAlignmentCenter;
    displayer.label.font=[UIFont systemFontOfSize:16.0];
    displayer.label.backgroundColor=KCOLOR_CLEAR;
    displayer.label.numberOfLines=0;
    [displayer addSubview:displayer.label];
    
    displayer.currentFrames=0;
    displayer.calculateTimes=20;
    displayer.accuracy=1;
    displayer.prefix=@"";
    displayer.suffix=@" fps";
    displayer.excellentFrames=56.0;
    displayer.goodFrames=42.0;
    displayer.normalFrames=28.0;
    displayer.badFrames=14.0;
    displayer.terribleFrames=0.0;
    displayer.excellentColor=KCOLOR_BLUE;
    displayer.goodColor=KCOLOR_GREEN;
    displayer.normalColor=KCOLOR_YELLOW;
    displayer.badColor=KCOLOR_ORANGE;
    displayer.terribleColor=KCOLOR_PINK;
    displayer.stoped=YES;
    
    displayer.timer=[CADisplayLink displayLinkWithTarget:displayer selector:@selector(calculate)];
    displayer.timer.frameInterval=1;
    
    //添加定时器对象到主运行循环
    [displayer.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [displayer.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
    
    return displayer;
}


#pragma -mark 对象方法

//开始计算
- (void)start
{
    _stoped=NO;
    _count=0;
    
    if(_label)
    {
        _label.text=@"";
    }
}


//停止计算
- (void)stop
{
    _stoped=YES;
    _count=0;
    
    if(_label)
    {
        _label.text=@"";
    }
}


//计算过程
- (void)calculate
{
    if(!_stoped)
    {
        if(_count>=_calculateTimes)
        {            
            //获取时间
            CFTimeInterval now=CACurrentMediaTime();
            CFTimeInterval frameDuration=now-_lastCalculateTime;
            
            _lastCalculateTime=now;
            
            _currentFrames=(_count+1)*1.0/frameDuration;//帧数
            
            if(_label)
            {
                if(_accuracy==1)
                {
                    _label.text=[NSString stringWithFormat:@"%@%.1f%@",_prefix,_currentFrames,_suffix];
                }
                else if(_accuracy==2)
                {
                    _label.text=[NSString stringWithFormat:@"%@%.2f%@",_prefix,_currentFrames,_suffix];
                }
                else
                {
                    _label.text=[NSString stringWithFormat:@"%@%.0f%@",_prefix,_currentFrames,_suffix];
                }
                
                if(_currentFrames>=_excellentFrames)
                {
                    _label.textColor=_excellentColor;
                }
                else if(_currentFrames>=_goodFrames)
                {
                    _label.textColor=_goodColor;
                }
                else if(_currentFrames>=_normalFrames)
                {
                    _label.textColor=_normalColor;
                }
                else if(_currentFrames>=_badFrames)
                {
                    _label.textColor=_badColor;
                }
                else if(_currentFrames>=_terribleFrames)
                {
                    _label.textColor=_terribleColor;
                }
            }
            
            _count=0;
        }
        else
        {
            _count++;
        }
    }
}


//从父视图中移除时，关闭定时器
- (void)removeFromSuperview
{
    [super removeFromSuperview];
    
    _stoped=YES;
    
    if(_timer)//取消定时器
    {
        [_timer invalidate];
        [_timer release];
        _timer=nil;
    }
    
}



//释放内存
- (void)dealloc
{
    NSLog(@"Frame Displayer is dealloc...");
    
    _stoped=YES;
    
    if(_timer)//取消定时器
    {
        [_timer invalidate];
        [_timer release];
        _timer=nil;
    }
    
    [_label release];
    [_prefix release];
    [_suffix release];
    [_excellentColor release];
    [_goodColor release];
    [_normalColor release];
    [_badColor release];
    [_terribleColor release];
    
    [super dealloc];
}

@end
